// BitmapEx.cpp : implementation file
//

#include "stdafx.h"
#include "PeekPocket.h"
#include "BitmapEx.h"

IMPLEMENT_DYNAMIC(CBitmapEx, CBitmap)

//////////////////////////////////////////////////////////////////////////
CBitmapEx::CBitmapEx()
{
}

//////////////////////////////////////////////////////////////////////////
CBitmapEx::~CBitmapEx()
{
	CBitmap::DeleteObject();
}

//////////////////////////////////////////////////////////////////////////
void CBitmapEx::Display(CDC *pDestDC, int X, int Y)
{
CDC dcMem;
HBITMAP hBmpOld;
BITMAP bmpData;
int iW, iH;

	//get bitmap object dimensions
	if(::GetObject(m_hObject, sizeof(bmpData), &bmpData) == 0)
	{
		//failed to get bitmap dimensions, no display
		return;
	}

	iW=bmpData.bmWidth;
	iH=bmpData.bmHeight;

	//display BMP:
	//1. create a DC that is compatible with the destination DC
	//2. select the bitmap object into that DC
	//3. call BitBlt to copy bitmap bits from compatible DC to destination DC
	if(TRUE == dcMem.CreateCompatibleDC(pDestDC))
	{
		hBmpOld=(HBITMAP)dcMem.SelectObject(m_hObject);
		if(hBmpOld != NULL)
		{
			pDestDC->BitBlt(X, Y, iW, iH, &dcMem, 0, 0, SRCCOPY);
			dcMem.SelectObject(hBmpOld);
		}
	}//compatible DC ok
}

//////////////////////////////////////////////////////////////////////////
void CBitmapEx::DisplayTransparent(CDC *pDestDC, int X, int Y, COLORREF crTrans)
{
CDC dcMem;
HBITMAP hBmpOld;
BITMAP bmpData;
int iW, iH;

	//get bitmap object dimensions
	if(::GetObject(m_hObject, sizeof(bmpData), &bmpData) == 0)
	{
		//failed to get bitmap dimensions, no display
		return;
	}

	iW=bmpData.bmWidth;
	iH=bmpData.bmHeight;

	//display BMP with transparency:
	//1. create a DC that is compatible with the destination DC
	//2. select the bitmap object into that DC
	//3. call TransparentBlt to copy bitmap bits from compatible DC to destination DC
	if(TRUE == dcMem.CreateCompatibleDC(pDestDC))
	{
		hBmpOld=(HBITMAP)dcMem.SelectObject(m_hObject);
		if(hBmpOld != NULL)
		{
			::TransparentBlt(pDestDC->GetSafeHdc(), X, Y, iW, iH, dcMem.GetSafeHdc(), 0, 0, iW, iH, (UINT)crTrans);
			dcMem.SelectObject(hBmpOld);
		}
	}//compatible DC ok
}

